欧美一区2区三区4区公司二百,国产精品婷婷午夜在线观看,自拍偷拍亚洲精品,国产美女诱惑一区二区

窗口間的數(shù)據(jù)傳遞(三)

?三.使用靜態(tài)類

??? 這個也是我們經(jīng)常要用到的一種數(shù)據(jù)交互方法。

?

?

下面是定義的一個類:

?

using System;

?

using System.Collections;

?

namespace ZZ

?

{

?

???? public class AppDatas

?

???? {

?

???????? private static ArrayList listData;

?

???????? static AppDatas()

?

???????? {

?

????????????? listData = new ArrayList();

?

????????????? listData.Add("DotNet");

?

????????????? listData.Add("C#");

?

????????????? listData.Add("Asp.net");

?

????????????? listData.Add("WebService");

?

????????????? listData.Add("XML");

?

???????? }

?

???????? public static ArrayList ListData

?

???????? {

?

????????????? get{return listData;}

?

???????? }

?

???????? public static ArrayList GetListData()

?

???????? {

?

????????????? return listData;

?

???????? }

?

???? }

?

}

?

上面包含了一個靜態(tài)類成員,listData,一個靜態(tài)構(gòu)造函數(shù)static AppDatas(),用來初始化listData的數(shù)據(jù)。還有一個靜態(tài)屬性ListData和一個靜態(tài)GetListData()方法,他們實現(xiàn)了同樣的功能就是返回listData

?

由于前面兩篇文章已經(jīng)講了很多,這里不細說了,下面是完整的代碼:

?

Form1.cs文件

?

using System;

?

using System.Drawing;

?

using System.Collections;

?

using System.ComponentModel;

?

using System.Windows.Forms;

?

namespace ZZ

?

{

?

???? public class Form1 : System.Windows.Forms.Form

?

???? {

?

???????? private System.Windows.Forms.Button buttonEdit;

?

???????? private System.Windows.Forms.ListBox listBoxFrm1;

?

???????? private System.ComponentModel.Container components = null;

?

???????? public Form1()

?

???????? {

?

????????????? InitializeComponent();

?

????????????? this.listBoxFrm1.DataSource = AppDatas.ListData;

?

?????????????

?

???????? }

?

???????? protected override void Dispose( bool disposing )

?

???????? {

?

????????????? if( disposing )

?

?????????????????? if(components != null)

?

?????????????????????? components.Dispose();

?

?? ???????????base.Dispose( disposing );

?

???????? }

?

???????? [STAThread]

?

???????? static void Main()

?

???????? {

?

????????????? Application.Run(new Form1());

?

???????? }

?

???????? private void InitializeComponent()

?

???????? {

?

????????????? this.buttonEdit = new System.Windows.Forms.Button();

?

????????????? this.listBoxFrm1 = new System.Windows.Forms.ListBox();

?

????????????? this.SuspendLayout();

?

????????????? this.buttonEdit.Location = new System.Drawing.Point(128, 108);

?

????????????? this.buttonEdit.Name = "buttonEdit";

?

????????????? this.buttonEdit.TabIndex = 1;

?

????????????? this.buttonEdit.Text = "修改";

?

????????????? this.buttonEdit.Click += new System.EventHandler(this.buttonEdit_Click);

?

????????????? this.listBoxFrm1.ItemHeight = 12;

?

??????? ??????this.listBoxFrm1.Location = new System.Drawing.Point(12, 8);

?

????????????? this.listBoxFrm1.Name = "listBoxFrm1";

?

????????????? this.listBoxFrm1.Size = new System.Drawing.Size(108, 124);

?

????????????? this.listBoxFrm1.TabIndex = 2;

?

????????????? this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

?

????????????? this.ClientSize = new System.Drawing.Size(208, 141);

?

????????????? this.Controls.Add(this.listBoxFrm1);

?

????????????? this.Controls.Add(this.buttonEdit);

?

????????????? this.Name = "Form1";

?

????????????? this.Text = "Form1";

?

????????????? this.ResumeLayout(false);

?

???????? }

?

???????? private void buttonEdit_Click(object sender, System.EventArgs e)

?

???????? {

?

????????????? Form2 formChild = new Form2();

?

????????????? formChild.ShowDialog();

?

????????????? this.listBoxFrm1.DataSource = null;

?

????????????? this.listBoxFrm1.DataSource = AppDatas.ListData;

?

???????? }

?

???? }

?

}

?

Form2.cs文件

?

using System.Drawing;

?

using System.Collections;

?

using System.ComponentModel;

?

using System.Windows.Forms;

?

namespace ZZ

?

{

?

???? public class Form2 : System.Windows.Forms.Form

?

???? {

?

???????? private System.Windows.Forms.Button buttonOK;

?

???????? private System.ComponentModel.Container components = null;

?

???????? private System.Windows.Forms.ListBox listBoxFrm2;

?

???????? private System.Windows.Forms.Button buttonAdd;

?

???????? private System.Windows.Forms.Button buttonDel;

?

???????? private System.Windows.Forms.TextBox textBoxAdd;

?

???????? public Form2()

?

???????? {

?

????????????? InitializeComponent();

?

????????????? foreach(object o in AppDatas.ListData)

?

?????????????????? this.listBoxFrm2.Items.Add(o);

?

???????? }

?

???????? protected override void Dispose( bool disposing )

?

???????? {

?

????????????? if( disposing )

?

?????????????????? if(components != null)

?

?????????????????????? components.Dispose();

?

????????????? base.Dispose( disposing );

?

???????? }

?

???????? private void InitializeComponent()

?

???????? {

?

????????????? this.buttonOK = new System.Windows.Forms.Button();

?

????????? ????this.listBoxFrm2 = new System.Windows.Forms.ListBox();

?

????????????? this.buttonAdd = new System.Windows.Forms.Button();

?

????????????? this.buttonDel = new System.Windows.Forms.Button();

?

????????????? this.textBoxAdd = new System.Windows.Forms.TextBox();

?

????????????? this.SuspendLayout();

?

????????????? this.buttonOK.Location = new System.Drawing.Point(188, 108);

?

????????????? this.buttonOK.Name = "buttonOK";

?

????????????? this.buttonOK.TabIndex = 0;

?

????????????? this.buttonOK.Text = "確定";

?

?? ???????????this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);

?

????????????? this.listBoxFrm2.ItemHeight = 12;

?

????????????? this.listBoxFrm2.Location = new System.Drawing.Point(8, 8);

?

????????????? this.listBoxFrm2.Name = "listBoxFrm2";

?

????????????? this.listBoxFrm2.Size = new System.Drawing.Size(168, 124);

?

????????????? this.listBoxFrm2.TabIndex = 2;

?

????????????? this.buttonAdd.Location = new System.Drawing.Point(188, 44);

?

????????????? this.buttonAdd.Name = "buttonAdd";

?

?????? ???????this.buttonAdd.TabIndex = 3;

?

????????????? this.buttonAdd.Text = "增加";

?

????????????? this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click);

?

????????????? this.buttonDel.Location = new System.Drawing.Point(188, 76);

?

????????????? this.buttonDel.Name = "buttonDel";

?

????????????? this.buttonDel.TabIndex = 4;

?

????????????? this.buttonDel.Text = "刪除";

?

????????????? this.buttonDel.Click += new System.EventHandler(this.buttonDel_Click);

?

????????????? this.textBoxAdd.Location = new System.Drawing.Point(188, 12);

?

????????????? this.textBoxAdd.Name = "textBoxAdd";

?

????????????? this.textBoxAdd.Size = new System.Drawing.Size(76, 21);

?

????????????? this.textBoxAdd.TabIndex = 5;

?

????????????? this.textBoxAdd.Text = "";

?

????????????? this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

?

????????????? this.ClientSize = new System.Drawing.Size(272, 141);

?

????????????? this.Controls.Add(this.textBoxAdd);

?

????????????? this.Controls.Add(this.buttonDel);

?

????????????? this.Controls.Add(this.buttonAdd);

?

????????????? this.Controls.Add(this.listBoxFrm2);

?

????????????? this.Controls.Add(this.buttonOK);

?

????????????? this.Name = "Form2";

?

????????????? this.Text = "Form2";

?

????????????? this.ResumeLayout(false);

?

???????? }

?

???????? private void buttonOK_Click(object sender, System.EventArgs e)

???????? {

????????????? this.Close();

???????? }

?

???????? private void buttonAdd_Click(object sender, System.EventArgs e)

?

???????? {

?

????????????? if(this.textBoxAdd.Text.Trim().Length>0)

?

? ????????????{

?

?????????????????? AppDatas.ListData.Add(this.textBoxAdd.Text.Trim());

?

?????????????????? this.listBoxFrm2.Items.Add(this.textBoxAdd.Text.Trim());

?

????????????? }

?

????????????? else

?

?????????????????? MessageBox.Show("請輸入添加的內(nèi)容!");

?

???? ?????????

?

???????? }

?

???????? private void buttonDel_Click(object sender, System.EventArgs e)

?

???????? {

?

????????????? int index = this.listBoxFrm2.SelectedIndex;

?

????????????? if(index!=-1)

?

????????????? {

?

??????????????????? AppDatas.ListData.RemoveAt(index);

?

?????????????????? this.listBoxFrm2.Items.RemoveAt(index);

?

????????????? }

?

????????????? else

?

?????????????????? MessageBox.Show("請選擇刪除項!");

?

???????? }

?

???? }

?

}???

?

??? 總結(jié),我認為使用靜態(tài)類比較多的地方就是把應(yīng)用程序的配置文件裝載到一個靜態(tài)類里面,讓所有的窗體和其他實例都可以通過靜態(tài)屬性以及靜態(tài)方法使用這些數(shù)據(jù),比如三層結(jié)構(gòu)或多層結(jié)構(gòu)都可以訪問它,而不是在多個實例間傳來傳去。在這里我們討論的是Windows窗體,其實在兩個不同的實例間交互數(shù)據(jù),都可以采用三篇文章中的方案實現(xiàn),除非是這個類特有的屬性或著方法。現(xiàn)在都講完了,雖然不是什么高深的東西,但是希望能對一些初學者有所幫助,同時也歡迎各位朋友進行技術(shù)交流,共同提高。

窗口間的數(shù)據(jù)傳遞(三)

文章鏈接: http://m.qzkangyuan.com/10751.html

文章標題:窗口間的數(shù)據(jù)傳遞(三)

文章版權(quán):夢飛科技所發(fā)布的內(nèi)容,部分為原創(chuàng)文章,轉(zhuǎn)載請注明來源,網(wǎng)絡(luò)轉(zhuǎn)載文章如有侵權(quán)請聯(lián)系我們!

聲明:本站所有文章,如無特殊說明或標注,均為本站原創(chuàng)發(fā)布。任何個人或組織,在未征得本站同意時,禁止復制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進行處理。

給TA打賞
共{{data.count}}人
人已打賞
建站教程投稿分享

窗口間的數(shù)據(jù)傳遞(二)

2022-9-29 21:01:34

建站教程投稿分享運維技術(shù)

An Easy Problem最簡單算法

2022-9-30 15:08:53

0 條回復 A文章作者 M管理員
    暫無討論,說說你的看法吧
?
個人中心
購物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索
主站蜘蛛池模板: 郎溪县| 克拉玛依市| 宜兰县| 枣庄市| 奈曼旗| 蓬安县| 永城市| 阳曲县| 买车| 吉林省| 二连浩特市| 塘沽区| 汉源县| 肥城市| 资阳市| 蓝田县| 麻江县| 嘉义市| 登封市| 福贡县| 邯郸县| 同心县| 洛隆县| 镇康县| 东宁县| 略阳县| 浙江省| SHOW| 长兴县| 和林格尔县| 云南省| 和田市| 巴彦县| 乌审旗| 莒南县| 克什克腾旗| 德庆县| 射阳县| 松滋市| 启东市| 江永县|